﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WSNs
{
    public partial class CtlNode : PictureBox
    {
        #region Fields

        public Action<AcqStructure,double> AcqStructChangeEvent;
        public Action<int> DeleteNodeEvent;

        private int _sizeD = 40;
        private int _penWidth = 7;
        private Pen _penG = new Pen(Color.MediumSeaGreen, 8); //网关画笔颜色
        private Pen _penRouter = new Pen(Color.DarkViolet, 8); //路由画笔颜色
        private Pen _penDevice = new Pen(Color.Navy, 8); //采集节点画笔颜色
        private Font _f = new Font("Verdana", 12, FontStyle.Bold);
        private Point _mouseDownPoint = new Point(0, 0);
        private AcqStructure _acqStruct = new AcqStructure();
        private ContextMenuStrip _ctMenu = new ContextMenuStrip();
        #endregion

        #region Properties

        /// <summary>
        /// 节点类型（网关、路由、采集节点）
        /// </summary>
        public NodesType NodeType { get; set; }

        /// <summary>
        /// 节点通道类型，用于区分不同的节点（不同节点数据位数、存储不一样）
        /// </summary>
        public ChannelType ChannelType { get; set; }

        /// <summary>
        /// 显示文本
        /// </summary>
        public override string Text { get; set; }

        /// <summary>
        /// 父节点
        /// </summary>
        public int FatherNode { get; set; }

        /// <summary>
        /// 传感器灵敏度
        /// </summary>
        public double Sensitivity { get; set; } = 1000;

        /// <summary>
        /// 本节点ID
        /// </summary>
        public int NodeID
        {
            get {
                try
                {
                    return int.Parse(Text);
                }
                catch
                {
                    if (Text == "G") return 0;
                    return -1; //ID错误
                }
            }
            set { Text = value.ToString(); }
        }

        /// <summary>
        /// 节点间线段连接热点
        /// </summary>
        public PointF HotPoint
        {
            get { return new PointF(Location.X + Width / 2, Location.Y + Height / 2); }
        }

        /// <summary>
        /// 采集参数配置
        /// </summary>
        public AcqStructure AcqStructure
        {
            get { return _acqStruct; }
            set { _acqStruct = value; }
        }

        #endregion

        #region Public

        public CtlNode()
        {
            AcqStructure = new AcqStructure()
            {
                FileID = 0,                
                CoupleMode = CoupleMode.AC,                
                AcqLength = 3000,
                Frequency = 100,
                AcqMode = AcqMode.Freedom,                
                StorageLocation = StorageLocation.MicroSD,
            };
            InitializeComponent();
            this.BackColor = Color.Transparent;
            _penG = new Pen(Color.MediumSeaGreen, _penWidth);
            Height = _sizeD + _penWidth;
            Width = _sizeD + _penWidth;
            this.DoubleBuffered = true;

            _ctMenu.Items.Add("删除");
            _ctMenu.ItemClicked += _ctMenu_ItemClicked;
            this.ContextMenuStrip = _ctMenu;
        }

        void _ctMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            //throw new NotImplementedException();
            DeleteNodeEvent(this.NodeID);
        }

        #endregion

        #region Override
        protected override void OnPaint(PaintEventArgs e)
        {
            Draw(e.Graphics);
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            //base.OnMouseDoubleClick(e);
            Form_TopoAndAcq frm_Topo = new Form_TopoAndAcq(Control.MousePosition,this);
            frm_Topo.ReConfigAcqEvent += Form_TopoAndAcq_ReConfigAcqEvent;
            frm_Topo.ShowDialog();
            frm_Topo.Dispose();
            OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            _mouseDownPoint = e.Location;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button != MouseButtons.Left) return;
            Location = new Point(e.X - _mouseDownPoint.X + Location.X, e.Y - _mouseDownPoint.Y + Location.Y);
            Invalidate();
        }


        #endregion

        #region Private

        private void Form_TopoAndAcq_ReConfigAcqEvent(CtlNode node)
        {
            this.FatherNode = node.FatherNode;
            this.AcqStructure = node.AcqStructure;
            this.Sensitivity = node.Sensitivity;
            if (node.NodeType == NodesType.EndNode)
                AcqStructChangeEvent(AcqStructure,node.Sensitivity); //配置发生变化时触发事件
        }
        private void Draw(Graphics g)
        {
            SizeF sizeF = g.MeasureString(Text, _f); //??
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.FillEllipse(Brushes.WhiteSmoke, _penWidth, _penWidth, _sizeD - _penWidth, _sizeD - _penWidth);
            switch (NodeType)
            {
                case WSNs.NodesType.Coordinator:
                    g.DrawEllipse(_penG, _penWidth / 2, _penWidth / 2, _sizeD, _sizeD);
                    break;
                case WSNs.NodesType.Router:
                    g.DrawEllipse(_penRouter, _penWidth / 2, _penWidth / 2, _sizeD, _sizeD);
                    break;
                case WSNs.NodesType.EndNode:
                    g.DrawEllipse(_penDevice, _penWidth / 2, _penWidth / 2, _sizeD, _sizeD);
                    break;
            }
            g.DrawString(Text, _f, Brushes.DarkRed, _sizeD/2 - sizeF.Width/2 + (float) _penWidth/2,
                _sizeD/2 - sizeF.Height/2 + (float) _penWidth/2);

        }

        #endregion

    }
}
